<template>
  <view class="page">
    <view class="head">
      <view class="head-name">
        <view style="margin-right: 10rpx">{{ detail.receiveName }}</view>
        <view>{{ detail.receivePhonenum }}</view>
      </view>
      <view class="head-adree">{{ detail.receiveAddress }}</view>
    </view>
    <view class="content">
      <view class="item">
        <view class="item-num">
          <view class="order-num"
            >订单编号：{{ detail.appletBdmOrderItemVos[0].id }}</view
          >
          <view class="order-type">{{ detail.orderStatusName }}</view>
        </view>
        <view
          class="m-20"
          v-for="(item, index) in detail.appletBdmOrderItemVos"
          :key="index"
        >
          <view class="flex">
            <image class="order-img" :src="baseImgUrl + item.goodImg" />
            <view class="order-content">
              <view class="item-title">{{ item.goodName }}</view>
              <view class="item-title1 m-33"
                >规格：{{ item.goodSpecs || "无规格" }}</view
              >
              <view class="item-price">
                <view>￥{{ item.goodPrice }}</view>
                <view>X {{ item.goodCount }}</view>
              </view>
            </view>
          </view>
          <view style="display: flex; justify-content: flex-end">
            <view
              class="evaluate"
              v-if="detail.orderStatus == 4"
              @click.stop="toEvaluate(item.supplierGoodId)"
              >去评价</view
            >
          </view>
        </view>
      </view>
    </view>
    <view class="sum-pirce">
      <view class="pirce-flex">
        <view class="pirce-text">价格</view>
        <view class="pirce-text1" v-if="detail.deductIntegral == 0"
          >￥{{ detail.payMoney - detail.deliveryCost}}</view
        >
        <view class="pirce-text1" v-else
          >积分{{ detail.deductIntegral }}+￥{{ detail.payMoney - detail.deliveryCost }}</view
        >
      </view>
      <view class="pirce-flex mt-40">
        <view class="pirce-text">运费</view>
        <view class="pirce-text2">￥{{ detail.deliveryCost }}</view>
      </view>
    </view>
    <view
      class="sum-pirce"
      :style="{ marginBottom: detail.orderStatus == 6 ? '40rpx' : '160rpx' }"
    >
      <view class="pirce-flex">
        <view class="pirce-text">订单编号</view>
        <view class="pirce-text2">{{ detail.id }}</view>
      </view>
      <view class="pirce-flex mt-40">
        <view class="pirce-text">支付方式</view>
        <view class="pirce-text2">{{
          detail.payType == 1
            ? "余额支付"
            : detail.payType == 2
            ? "微信支付"
            : "未支付"
        }}</view>
      </view>
      <view class="pirce-flex mt-40">
        <view class="pirce-text">下单时间</view>
        <view class="pirce-text2">{{ detail.createTime }}</view>
      </view>
      <view class="pirce-flex mt-40">
        <view class="pirce-text">配送方式</view>
        <view class="pirce-text2">{{
          detail.deliveryType == 1
            ? "普通配送"
            : detail.deliveryType == 2
            ? "加急配送"
            : ""
        }}</view>
      </view>
    </view>
    <view class="btn">
      <template v-if="detail.orderStatus == 0">
        <view class="btn1" @click.stop="cancelOrder">取消订单</view>
        <view class="btn2" @click.stop="toPay">去付款</view>
      </template>
      <template v-if="detail.orderStatus == 1">
        <view class="btn1" @click.stop="toggle">我要退货</view>
      </template>
      <template v-if="detail.orderStatus == 2">
        <view class="btn1" @click.stop="toggle">我要退货</view>
        <view class="btn1" @click.stop="toLogistics">查看物流</view>
        <view class="btn2" @click.stop="receivingGoods">确认收货</view>
      </template>
      <template v-if="detail.orderStatus == 4">
        <view class="btn1" @click.stop="toLogistics">查看物流</view>
      </template>
      <template v-if="detail.orderStatus == 5 || detail.orderStatus == 7">
        <view class="btn1" @click.stop="toSchedule">查看进度</view>
      </template>
    </view>

    <uni-popup
      ref="popup"
      background-color="rgba(0,0,0,0)"
      :safeArea="false"
      type="bottom"
    >
      <view class="popup-content">
        <view class="popup-title">退货申请</view>
        <view class="popup-flex">
          <view class="flex">
            <view class="popup-text">退货原因</view>
            <view class="popup-text1">*</view>
          </view>
          <input
            class="popup-input"
            v-model="refundReason"
            type="text"
            placeholder="请输入"
          />
        </view>

        <view class="popup-flex">
          <view class="flex">
            <view class="popup-text">退款金额</view>
            <view class="popup-text1">*</view>
          </view>
          <view>￥{{ detail.payMoney }}</view>
        </view>
        <view class="submit" @click="chargeback">提交申请</view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import orderApi from "@/api/order";
import { filterEnum } from "@/utils/index";
import { baseImgUrl } from "@/config";

export default {
  name: "OrderDetail",
  components: {},
  data() {
    return {
      baseImgUrl,
      id: null,
      detail: {},
      orderStatusEnum: [
        {
          value: 0,
          name: "待付款",
        },
        {
          value: 1,
          name: "待发货",
        },
        {
          value: 2,
          name: "已发货",
        },
        {
          value: 3,
          name: "待收货",
        },
        {
          value: 4,
          name: "已完成", //
        },
        {
          value: 5,
          name: "已退单",
        },
        {
          value: 6,
          name: "已关闭",
        },
        {
          value: 7,
          name: "退货中",
        },
      ],
      refundReason: "", // 退货原因
    };
  },
  onLoad(options) {
    this.id = options.id || null;
  },
  onShow() {
    this.getOrderDetail();
  },
  methods: {
    getOrderDetail() {
      let data = {
        orderId: this.id,
      };
      orderApi.getOrderDetail(data).then((res) => {
        if (res.code == 200) {
          let ifEvaluate = res.data.appletBdmOrderItemVos.every(
            (item) => item.ifEvaluate == 1
          );
          this.detail = {
            ...res.data,
            ifEvaluate,
            orderStatusName: filterEnum(
              res.data.orderStatus,
              this.orderStatusEnum
            ),
          };
        }
      });
    },
    cancelOrder() {
      uni.showModal({
        title: "提示",
        content: "确认取消该订单吗？",
        cancelText: "取消",
        confirmText: "确定",
        success: (modalRes) => {
          if (modalRes.confirm) {
            let data = {
              orderId: this.id,
            };
            orderApi.cancelOrder(data).then((res) => {
              if (res.code == 200) {
                uni.showToast({
                  title: "取消成功！",
                  icon: "none",
                });
                this.getOrderDetail();
              }
            });
          }
        },
      });
    },
    toPay() {
		  uni.redirectTo({
		  url: `/subPages/details/confirmOrder?source=order&orderId=${this.detail.id}&id=${this.detail.appletBdmOrderItemVos[0].supplierGoodId}&integral=${this.detail.appletBdmOrderItemVos[0].deductIntegral}&price=${this.detail.appletBdmOrderItemVos[0].price}`,
		  });
    },
    toggle() {
      this.$refs.popup.open();
    },
    toEvaluate(supplierGoodId) {
      uni.navigateTo({
        url: `/subPages/myOrder/evaluate?orderId=${this.id}&itemId=${this.detail.id}&goodId=${supplierGoodId}`,
      });
    },
    toLogistics() {
      uni.navigateTo({
        url: `/subPages/myOrder/logistics?id=${this.id}`,
      });
    },
    toSchedule() {
      uni.navigateTo({
        url: `/subPages/myOrder/returnDetails?id=${this.id}&payMoney=${this.detail.payMoney}&orderStatus=${this.detail.orderStatus}`,
      });
    },
    receivingGoods() {
      let data = {
        orderId: this.id,
      };
      orderApi.receivingGoods(data).then((res) => {
        if (res.code == 200) {
          uni.showToast({
            title: "收货成功！",
            icon: "none",
          });
          this.getOrderDetail();
        }
      });
    },
    chargeback() {
      if (this.refundReason == "") {
        uni.showToast({
          title: "请输入退货原因",
          icon: "none",
        });
        return;
      }
      let data = {
        chargebackReason: this.refundReason,
        orderId: this.id,
      };
      orderApi.chargeback(data).then((res) => {
        if (res.code == 200) {
          uni.showToast({
            title: "退货申请成功",
            icon: "none",
          });
          setTimeout(() => {
            this.$refs.popup.close();
            uni.navigateTo({
              url: `/subPages/myOrder/returnDetails?id=${this.id}&payMoney=${this.detail.payMoney}&orderStatus=${this.detail.orderStatus}`,
            });
          }, 1500);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  width: 100vw;
  height: 100vh;
  overflow-y: auto;
}
.head {
  width: 694rpx;
  border-radius: 24rpx;
  background: #fff;
  margin: 20rpx 28rpx;
  padding: 20rpx 24rpx;

  .head-name {
    display: flex;
    color: #000000;
    font-family: "PingFang SC";
    font-size: 28rpx;
    font-style: normal;
    font-weight: 500;
    line-height: 44rpx;
    opacity: 0.9;
  }

  .head-adree {
    color: #000000;
    font-family: "PingFang SC";
    font-size: 24rpx;
    font-style: normal;
    font-weight: 400;
    line-height: 44rpx;
    opacity: 0.5;
    margin-top: 8rpx;
  }
}

.content {
  width: 694rpx;
  flex-shrink: 0;
  border-radius: 24rpx;
  background: #fff;
  margin: 0 24rpx;
  padding: 20rpx 24rpx;

  .item {
    .item-num {
      display: flex;
      justify-content: space-between;
      margin: 0 0 32rpx;

      .order-num {
        color: #000000;
        font-family: "PingFang SC";
        font-size: 24rpx;
        font-style: normal;
        font-weight: 400;
        line-height: normal;
        opacity: 0.5;
      }

      .order-type {
        color: #ff4254;
        text-align: right;
        font-family: "PingFang SC";
        font-size: 24rpx;
        font-style: normal;
        font-weight: 400;
        line-height: normal;
      }
    }
  }

  .order-img {
    width: 180rpx;
    height: 180rpx;
    border-radius: 24rpx;
    // border: 1rpx solid #000;
  }

  .order-content {
    width: calc(100% - 208rpx);
    margin-left: 28rpx;
  }

  .item-title {
    color: #000000;
    font-family: "PingFang SC";
    font-size: 28rpx;
    font-style: normal;
    font-weight: 500;
    line-height: 40rpx;
    opacity: 0.9;
  }

  .item-title1 {
    color: #000000;
    font-family: "PingFang SC";
    font-size: 24rpx;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
    opacity: 0.5;
  }

  .item-price {
    display: flex;
    justify-content: space-between;
  }
}

.m-20 {
  margin: 20rpx 0;
}

.flex {
  display: flex;
}

.sum-pirce {
  width: 694rpx;
  // height: 160rpx;
  flex-shrink: 0;
  border-radius: 24rpx;
  background: #fff;
  margin: 20rpx 28rpx;
  padding: 20rpx 24rpx;

  .pirce-flex {
    display: flex;
    justify-content: space-between;
  }

  .pirce-text {
    color: #000000;
    font-family: "PingFang SC";
    font-size: 28rpx;
    font-style: normal;
    font-weight: 400;
    line-height: 40rpx;
    opacity: 0.5;
  }

  .pirce-text1 {
    color: #000000;
    font-family: "PingFang SC";
    font-size: 28rpx;
    font-style: normal;
    font-weight: 600;
    line-height: 40rpx;
    opacity: 0.9;
  }

  .pirce-text2 {
    color: #000000;
    text-align: right;
    font-family: "PingFang SC";
    font-size: 28rpx;
    font-style: normal;
    font-weight: 400;
    line-height: 40rpx;
    opacity: 0.9;
  }
}

.mt-40 {
  margin-top: 40rpx;
}

.btn {
  position: fixed;
  bottom: 0;
  width: 750rpx;
  height: 160rpx;
  flex-shrink: 0;
  border-top: 1rpx solid #0000001a;
  background: #fff;
  padding: 0 28rpx;
  display: flex;
  align-items: center;
  justify-content: flex-end;

  .btn1 {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 218rpx;
    height: 100rpx;
    margin-left: 10rpx;
    flex-shrink: 0;
    border-radius: 50rpx;
    border: 2.5rpx solid #121212;
    color: #121212;
    text-align: center;
    font-family: "PingFang SC";
    font-size: 24rpx;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
  }

  .btn2 {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 218rpx;
    height: 100rpx;
    margin-left: 10rpx;
    border-radius: 50rpx;
    background: #121212;
    color: #ffffff;
    text-align: center;
    font-family: "PingFang SC";
    font-size: 24rpx;
    font-style: normal;
    font-weight: 400;
    line-height: normal;
  }
}

.evaluate {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 120rpx;
  height: 60rpx;
  border-radius: 50rpx;
  background: #121212;
  color: #ffffff;
  text-align: center;
  font-family: "PingFang SC";
  font-size: 24rpx;
  font-style: normal;
  font-weight: 400;
  line-height: normal;
}

.popup-content {
  border-top-left-radius: 30rpx;
  border-top-right-radius: 30rpx;
  background: #fff;
  padding-bottom: 34rpx;
  padding-top: 40rpx;

  .popup-title {
    display: flex;
    justify-content: center;
    align-items: center;
    color: #000000;
    text-align: center;
    font-family: "PingFang SC";
    font-size: 32rpx;
    font-style: normal;
    font-weight: 500;
    line-height: normal;
  }

  .popup-flex {
    display: flex;
    justify-content: space-between;
    padding: 0 28rpx;
    margin-top: 60rpx;

    .popup-text {
      color: #000000;
      font-family: "PingFang SC";
      font-size: 28rpx;
      font-style: normal;
      font-weight: 500;
      line-height: 40rpx;
    }

    .popup-text1 {
      color: red;
    }

    .popup-input {
      text-align: right;
    }
  }

  .submit {
    width: 642rpx;
    height: 100rpx;
    flex-shrink: 0;
    border-radius: 50rpx;
    background: #121212;
    margin: 0 auto;
    margin-top: 475rpx;
    margin-bottom: 30rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #ffffff;
    text-align: center;
    font-family: "PingFang SC";
    font-size: 24rpx;
    font-style: normal;
    font-weight: 400;
  }
}
</style>
